<?php

namespace app\admin\controller\ai;

use app\common\controller\BaseController;
use hg\apidoc\annotation as Apidoc;
use app\common\service\ai\SystemConfigService;

/**
 * @Apidoc\Title("系统配置管理")
 * @Apidoc\Group("ai")
 * @Apidoc\Sort("300")
 */
class SystemConfig extends BaseController {

    /**
     * @Apidoc\Title("列表")
     * @Apidoc\Returned("list", type="array", desc="列表", children={
     * @Apidoc\Returned(ref="app\common\model\ai\SystemConfigModel", withoutField="sdc"),
     * })
     */
    public function getList() {
        $type = $this->param('type') ?: 3;
        $list = SystemConfigService::getListByGroup($type);
        $group = SystemConfigService::getAllGroup();
        foreach ($list as &$item) {
            if ($item['name'] === 'AGREEMENT_CONTENT') {
                $item['value'] = htmlspecialchars_decode($item['value']);
            }
        }
        $result = [
            'list' => $list,
            'group' => $group,
            'type' => $type,
        ];
        return success($result);
    }

    /**
     * @Apidoc\Title("保存配置")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\ai\SystemConfigModel", field="config")
     * })
     */
    public function save() {
        $result = SystemConfigService::save(json_decode($this->param('config'), true));
        return $result ? success() : error();
    }

    /**
     * @Apidoc\Title("刷新缓存")
     * @Apidoc\Method("POST")
     * })
     */
    public function refresh() {
        $result = SystemConfigService::refresh();
        return $result ? success() : error();
    }

    /**
     * 改变状态
     */
    public function changeStatus() {
        return error('没权限更改');
    }

}
